package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;


import com.atguigu.yygh.vo.hosp.ScheduleOrderVo;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("/schedule/remove")
    public Result remove(HttpServletRequest request){
        //获取hospital-manage传递信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //不想要String[]想变成Object[]
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        //获取医院系统中传递的签名,签名进行了md5加密
        String hospsign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查找数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名做md5加密
        String signFromMysql = MD5.encrypt(signKey);
        //判断
        if(!hospsign.equals(signFromMysql)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();


    }

    //查询排班
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取hospital-manage传递信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //不想要String[]想变成Object[]
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ?
                1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ?
                1 : Integer.parseInt((String) paramMap.get("limit"));

        //获取医院系统中传递的签名,签名进行了md5加密
        String hospsign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查找数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名做md5加密
        String signFromMysql = MD5.encrypt(signKey);
        //判断
        if(!hospsign.equals(signFromMysql)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleOrderVo scheduleOrderVo = new ScheduleOrderVo();
        scheduleOrderVo.setHoscode(hoscode);
        scheduleOrderVo.setHoscode(depcode);
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleOrderVo);
        return Result.ok(pageModel);

    }

    //上传排版
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取hospital-manage传递信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //不想要String[]想变成Object[]
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        //获取医院系统中传递的签名,签名进行了md5加密
        String hospsign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查找数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名做md5加密
        String signFromMysql = MD5.encrypt(signKey);
        //判断
        if(!hospsign.equals(signFromMysql)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();


    }

    //删除科室接口
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取hospital-manage传递信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //不想要String[]想变成Object[]
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //获取医院系统中传递的签名,签名进行了md5加密
        String hospsign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查找数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名做md5加密
        String signFromMysql = MD5.encrypt(signKey);
        //判断
        if(!hospsign.equals(signFromMysql)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode,depcode);
        return Result.ok();

    }


    //查询科室接口
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取hospital-manage传递信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //不想要String[]想变成Object[]
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号

        String hoscode = (String) paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ?
                1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ?
                1 : Integer.parseInt((String) paramMap.get("limit"));

        //获取医院系统中传递的签名,签名进行了md5加密
        String hospsign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查找数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名做md5加密
        String signFromMysql = MD5.encrypt(signKey);
        //判断
        if(!hospsign.equals(signFromMysql)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);

    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取hospital-manage传递信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //不想要String[]想变成Object[]
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取医院系统中传递的签名,签名进行了md5加密
        String hospsign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查找数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名做md5加密
        String signFromMysql = MD5.encrypt(signKey);
        //判断
        if(!hospsign.equals(signFromMysql)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service
        departmentService.save(paramMap);
        return Result.ok();
    }


    //从mongodb中查询医院
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取hospital-manage传递信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //不想要String[]想变成Object[]
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //获取医院系统中传递的签名,签名进行了md5加密
        String hospsign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查找数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名做md5加密
        String signFromMysql = MD5.encrypt(signKey);
        //判断
        if(!hospsign.equals(signFromMysql)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);

    }



    //上传医院的接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取hospital-manage传递信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //不想要String[]想变成Object[]
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院系统中传递的签名,签名进行了md5加密
        String hospsign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查找数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名做md5加密
        String signFromMysql = MD5.encrypt(signKey);
        //判断
        if(!hospsign.equals(signFromMysql)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //图片空格加号转换
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);


        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }
}
